package com.dgut.controller;

import com.dgut.dto.CustomerPageDTO;
import com.dgut.entity.Customers;
import com.dgut.result.PageResult;
import com.dgut.result.Result;
import com.dgut.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customer")
@Api(tags = "客户管理接口")
@Slf4j
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /**
     * 分页查询客户信息
     *
     * @param customerPageDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询客户信息")
    public Result pageQuery(CustomerPageDTO customerPageDTO) {
        log.info("分页查询客户信息: 参数customerDTO:{}", customerPageDTO);
        PageResult pageResult = customerService.pageQuery(customerPageDTO);
        return Result.success(pageResult);
    }


    /**
     * 查询客户数量
     * @return
     */
    @GetMapping("/count")
    @ApiOperation("查询客户数量")
    public Result<Long> count() {
        log.info("查询客户数量");
        Long count = customerService.count();
        return Result.success(count);
    }

    /**
     * 新增客户
     *
     * @param customers
     * @return
     */
    @ApiOperation("新增客户")
    @PostMapping("/save")
    public Result save(@RequestBody Customers customers) {
        log.info("新增客户: 参数customers:{}", customers);
        customerService.save(customers);
        return Result.success();
    }

    /**
     * 删除客户
     *
     * @param ids
     * @return
     */
    @ApiOperation("删除客户")
    @DeleteMapping("/delete")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("删除客户: 参数ids:{}", ids);
        customerService.delete(ids);
        return Result.success();
    }

    /**
     * 修改客户
     *
     * @param customers
     * @return
     */
    @ApiOperation("修改客户")
    @PutMapping("/update")
    public Result update(@RequestBody Customers customers) {
        log.info("修改客户: 参数customers:{}", customers);
        customerService.update(customers);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("查询所有客户")
    public Result list() {
        log.info("查询所有客户信息");
        List<Customers> list = customerService.list();
        return Result.success(list);
    }
}
